package app.widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longevityassistant.activity.R;

/**
 * Menu对话框
 * @author EX-XIAOFANQING001
 * @date 2012-1-6
 * @version [Android PABank C01, @2012-1-6]
 * @description
 */
public class MenuDialog extends BaseDialog
{
    
    public static final String TAG = MenuDialog.class.getSimpleName();
    
    public static final int GRAVITY_LEFT = 0;
    public static final int GRAVITY_CENTER = 1;
    
    
    private LinearLayout contentView;
    
    private OnMenuItemClickListener onMenuItemClickListener;
    
    private LinearLayout llMenusContainer;
    
    private int mTextGravity = GRAVITY_LEFT;
    
    public MenuDialog(Context context)
    {
        super(context);
        
        contentView =  (LinearLayout)LayoutInflater.from(getContext()).inflate(R.layout.app_widget_menu_dialog, null);
        llMenusContainer = (LinearLayout) contentView.findViewById(R.id.ll_menus_container);
        setBtn1Visible(false);
        setBtn1Text(R.string.cancel);
    }
    
    public MenuDialog(Context context, String[] menus) {
        this(context);
        setMenus(menus);
    }
    
    public MenuDialog(Context context, List<String> menus) {
        this(context);
        setMenus(menus);
    }
    
    public MenuDialog(Context context, List<String> menus, OnMenuItemClickListener onMenuItemClickListener) {
        this(context);
        setMenus(menus);
        this.onMenuItemClickListener = onMenuItemClickListener;
    }
    
    public MenuDialog(Context context, String[] menus, int textGravity) {
        this(context);
        setMenus(menus, textGravity);
    }
    
    public MenuDialog(Context context, List<String> menus, int textGravity) {
        this(context);
        setMenus(menus, textGravity);
    }
    
    public MenuDialog(Context context, List<String> menus, int textGravity, OnMenuItemClickListener onMenuItemClickListener) {
        this(context);
        setMenus(menus, textGravity);
        this.onMenuItemClickListener = onMenuItemClickListener;
    }

    @Override
    public View createContentView()
    {
        return contentView;
    }
    
    public void setMenus(List<String> menus, int textGravity) {
        if (textGravity == GRAVITY_LEFT) {
            mTextGravity = Gravity.CENTER_VERTICAL | Gravity.LEFT;
        } else {
            mTextGravity = Gravity.CENTER;
        }
        
        llMenusContainer.removeAllViews();
        if (menus != null) {
            for (int i=0; i<menus.size(); i++) {
                
                TextView tvMenu = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.app_widget_menu_dialog_item, null);
                tvMenu.setGravity(mTextGravity);
                tvMenu.setText(menus.get(i));
                final int pos = i;
                tvMenu.setOnClickListener(new View.OnClickListener()
                {  
                    @Override
                    public void onClick(View v)
                    {
                        if (onMenuItemClickListener != null ) {
                            onMenuItemClickListener.onMenuItemClick(pos);
                        }
                        dismiss();
                    }
                });
                
                llMenusContainer.addView(tvMenu);
            }
        }
    }
    
    public void setMenus(String[] menus, int textGravity) {
        List<String> listMenus = new ArrayList<String>();
        for (int i=0; i<menus.length; i++) {
            listMenus.add(menus[i]);
        }
        setMenus(listMenus);  
    }
    
    public void setMenus(List<String> menus) {
        setMenus(menus, GRAVITY_LEFT);
    }
    
    public void setMenus(String[] menus) {
        setMenus(menus, GRAVITY_LEFT);
    }
    
    public void setOnMenuItemClickListener(OnMenuItemClickListener onMenuItemClickListener)
    {
        this.onMenuItemClickListener = onMenuItemClickListener;
    }

    public interface OnMenuItemClickListener {
        public void onMenuItemClick(int position);
    }
}

